﻿using System.Windows.Input;
using ToDoListReminder.Data;
using SWF = System.Windows.Forms;

namespace ToDoListReminder.ViewModel
{
	/// <summary>
	/// The config window view model.
	/// </summary>
	public class ConfigWindowView
	{
		#region Fields

		/// <summary>
		/// Holds the command add.
		/// </summary>
		private EditionCommand commandAdd_;

		/// <summary>
		/// Holds the command edit.
		/// </summary>
		private EditionCommand commandEdit_;

		/// <summary>
		/// Holds the command delete.
		/// </summary>
		private EditionCommand commandDelete_;

		/// <summary>
		/// Holds the command OK.
		/// </summary>
		private EditionCommand commandOk_;

		/// <summary>
		/// Reference to window.
		/// </summary>
		private readonly ConfigWindow window_;
		#endregion

		#region Properties

		/// <summary>
		/// Gets the command add.
		/// </summary>
		public ICommand CommandAdd
		{
			get
			{
				if (commandAdd_ == null)
				{
					commandAdd_ = new EditionCommand();
					commandAdd_.OnCanExecute += CommandAddOnCanExecute;
					commandAdd_.OnExecute += CommandAddOnExecute;
				}

				return commandAdd_;
			}
		}

		/// <summary>
		/// Gets the command edit.
		/// </summary>
		public ICommand CommandEdit
		{
			get
			{
				if (commandEdit_ == null)
				{
					commandEdit_ = new EditionCommand();
					commandEdit_.OnCanExecute += CommandEditOnCanExecute;
					commandEdit_.OnExecute += CommandEditOnExecute;
				}

				return commandEdit_;
			}
		}

		/// <summary>
		/// Gets the command delete.
		/// </summary>
		public ICommand CommandDelete
		{
			get
			{
				if (commandDelete_ == null)
				{
					commandDelete_ = new EditionCommand();
					commandDelete_.OnCanExecute += CommandDeleteOnCanExecute;
					commandDelete_.OnExecute += CommandDeleteOnExecute;
				}

				return commandDelete_;
			}
		}

		/// <summary>
		/// Gets the command OK.
		/// </summary>
		public ICommand CommandOk
		{
			get
			{
				if (commandOk_ == null)
				{
					commandOk_ = new EditionCommand();
					commandOk_.OnCanExecute += CommandOkOnCanExecute;
					commandOk_.OnExecute += CommandOkOnExecute;
				}

				return commandOk_;
			}
		}

		/// <summary>
		/// Gets or sets the Config.
		/// </summary>
		public Config Config
		{
			get; set;
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ConfigWindowView class.
		/// </summary>
		/// <param name="window">Refence to window.</param>
		/// <param name="config">The config reference.</param>
		public ConfigWindowView(ConfigWindow window, Config config)
		{
			window_ = window;
			Config = config;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Executes the command add.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		private void CommandAddOnExecute(object parameter)
		{
			var openFileDialog = new SWF.OpenFileDialog
			{
				DefaultExt = ".tdl",
				Filter = "ToDoList files (*.tdl;*.xml)|*.tdl;*.xml",
				Title = "Select project"
			};

			if (openFileDialog.ShowDialog() == SWF.DialogResult.OK)
			{
				Config.Projects.Add(new Project(openFileDialog.FileName));
			}
		}

		/// <summary>
		/// Checks, if the command config may be executed.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		/// <returns>True, if the command may be executed.</returns>
		private static bool CommandAddOnCanExecute(object parameter)
		{
			return true;
		}

		/// <summary>
		/// Executes the command edit.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		private void CommandEditOnExecute(object parameter)
		{
			var item = window_.listBoxProjects.SelectedItem as Project;
			
			if (item != null)
			{
				var openFileDialog = new SWF.OpenFileDialog
				                     	{
				                     		DefaultExt = ".tdl",
				                     		Filter = "ToDoList files (*.tdl;*.xml)|*.tdl;*.xml",
				                     		Title = "Select project",
				                     		FileName = item.FileName
				                     	};

				if (openFileDialog.ShowDialog() == SWF.DialogResult.OK)
				{
					item.FileName = openFileDialog.FileName;
				}
			}
		}

		/// <summary>
		/// Checks, if the command config may be executed.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		/// <returns>True, if the command may be executed.</returns>
		private bool CommandEditOnCanExecute(object parameter)
		{
			return window_.listBoxProjects.SelectedItem != null;
		}

		/// <summary>
		/// Executes the command delete.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		private void CommandDeleteOnExecute(object parameter)
		{
			var item = window_.listBoxProjects.SelectedItem as Project;
			if (item != null)
			{
				Config.Projects.Remove(item);
			}
		}

		/// <summary>
		/// Checks, if the command config may be executed.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		/// <returns>True, if the command may be executed.</returns>
		private bool CommandDeleteOnCanExecute(object parameter)
		{
			return window_.listBoxProjects.SelectedItem != null;
		}

		/// <summary>
		/// Executes the command OK.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		private void CommandOkOnExecute(object parameter)
		{
			window_.DialogResult = true;
			window_.Close();
		}

		/// <summary>
		/// Checks, if the command config may be executed.
		/// </summary>
		/// <param name="parameter">The command data.</param>
		/// <returns>True, if the command may be executed.</returns>
		private static bool CommandOkOnCanExecute(object parameter)
		{
			return true;
		}

		#endregion

	}
}
